<!--$Id: dbm.so,v 10.26 2003/10/18 19:15:48 bostic Exp $-->
<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB: dbm/ndbm</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<table width="100%"><tr valign=top>
<td>
<b>dbm/ndbm</b>
</td>
<td align=right>
<a href="../api_c/api_core.html"><img src="../images/api.gif" alt="API"></a>
<a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a></td>
</tr></table>
<hr size=1 noshade>
<tt>
<b><pre>
#define DB_DBM_HSEARCH    1
#include &lt;db.h&gt;
<p>
typedef struct {
	char *dptr;
	int dsize;
} datum;
<hr size=1 noshade>
<b>Dbm Functions</b>
int
dbminit(char *file);
<p>
int
dbmclose();
<p>
datum
fetch(datum key);
<p>
int
store(datum key, datum content);
<p>
int
delete(datum key);
<p>
datum
firstkey(void);
<p>
datum
nextkey(datum key);
<hr size=1 noshade>
<b>Ndbm Functions</b>
DBM *
dbm_open(char *file, int flags, int mode);
<p>
void
dbm_close(DBM *db);
<p>
datum
dbm_fetch(DBM *db, datum key);
<p>
int
dbm_store(DBM *db, datum key, datum content, int flags);
<p>
int
dbm_delete(DBM *db, datum key);
<p>
datum
dbm_firstkey(DBM *db);
<p>
datum
dbm_nextkey(DBM *db);
<p>
int
dbm_error(DBM *db);
<p>
int
dbm_clearerr(DBM *db);
</pre></b>
<hr size=1 noshade>
<b>Description: dbm/ndbm</b>
<p>The dbm functions are intended to provide high-performance
implementations and source code compatibility for applications written
to historic interfaces.  They are not recommended for any other purpose.
The historic dbm database format <b>is not supported</b>,
and databases previously built using the real dbm libraries
cannot be read by the Berkeley DB functions.</p>
<p>To compile dbm applications, replace the application's
<b>#include</b> of the dbm or ndbm include file (for example,
<b>#include &lt;dbm.h&gt;</b> or <b>#include &lt;ndbm.h&gt;</b>)
with the following two lines:</p>
<blockquote><pre>#define DB_DBM_HSEARCH    1
#include &lt;db.h&gt;</pre></blockquote>
<p>and recompile.  If the application attempts to load against a dbm library
(for example, <b>-ldbm</b>), remove the library from the load line.</p>
<p><b>Key</b> and <b>content</b> parameters are objects described by the
<b>datum</b> typedef.  A <b>datum</b> specifies a string of
<b>dsize</b> bytes pointed to by <b>dptr</b>.  Arbitrary binary data,
as well as normal text strings, are allowed.</p>
<b>Dbm Functions</b>
<p>Before a database can be accessed, it must be opened by dbminit.
This will open and/or create the database <b>file</b>.db.  If created,
the database file is created read/write by owner only (as described in
<b>chmod</b>(2)) and modified by the process' umask value at the time
of creation (see <b>umask</b>(2)).  The group ownership of created
files is based on the system and directory defaults, and is not further
specified by Berkeley DB.</p>
<p>A database may be closed, and any held resources released, by calling
dbmclose.</p>
<p>Once open, the data stored under a key is accessed by fetch, and
data is placed under a key by store.  A key (and its associated
contents) are deleted by delete.  A linear pass through all keys
in a database may be made, in an (apparently) random order, by using
firstkey and nextkey.  The firstkey method will return
the first key in the database.  The nextkey method will return the next
key in the database.</p>
<p>The following code will traverse the database:</p>
<blockquote><pre>for (key = firstkey();
    key.dptr != NULL; key = nextkey(key)) {
	...
}</pre></blockquote>
<b>Ndbm Functions</b>
<p>Before a database can be accessed, it must be opened by dbm_open.
This will open and/or create the database file <b>file.db</b>, depending
on the flags parameter (see <b>open</b>(2)).  If created, the database
file is created with mode <b>mode</b> (as described in <b>chmod</b>(2)) and modified by the process' umask value at the time of creation (see
<b>umask</b>(2)).  The group ownership of created files is based on
the system and directory defaults, and is not further specified by
Berkeley DB.</p>
<p>Once open, the data stored under a key is accessed by dbm_fetch,
and data is placed under a key by dbm_store.  The <b>flags</b>
field can be either <b>DBM_INSERT</b> or <b>DBM_REPLACE</b>.
<b>DBM_INSERT</b> will only insert new entries into the database, and will
not change an existing entry with the same key.  <b>DBM_REPLACE</b> will
replace an existing entry if it has the same key.  A key (and its
associated contents) are deleted by dbm_delete.  A linear pass
through all keys in a database may be made, in an (apparently) random
order, by using dbm_firstkey and dbm_nextkey.  The
dbm_firstkey method will return the first key in the database.  The
dbm_nextkey method will return the next key in the database.</p>
<p>The following code will traverse the database:</p>
<blockquote><pre>for (key = dbm_firstkey(db);
    key.dptr != NULL; key = dbm_nextkey(db)) {
	...
}</pre></blockquote>
<b>Compatibility Notes</b>
<p>The historic dbm library created two underlying database files,
traditionally named <b>file.dir</b> and <b>file.pag</b>.  The Berkeley DB
library creates a single database file named <b>file.db</b>.
Applications that are aware of the underlying database filenames may
require additional source code modifications.</p>
<p>The historic dbminit function required that the underlying
<b>.dir</b> and <b>.pag</b> files already exist (empty databases were
created by first manually creating zero-length <b>.dir</b> and
<b>.pag</b> files).  Applications that expect to create databases using
this method may require additional source code modifications.</p>
<p>The historic dbm_dirfno and dbm_pagfno macros are
supported, but will return identical file descriptors because there is
only a single underlying file used by the Berkeley DB hashing access method.
Applications using both file descriptors for locking may require
additional source code modifications.</p>
<p>If applications using the dbm function exits without first
closing the database, it may lose updates because the Berkeley DB library
buffers writes to underlying databases.  Such applications will require
additional source code modifications to work correctly with the Berkeley DB
library.</p>
<b>Dbm Diagnostics</b>
<p>The dbminit function returns -1 on failure, setting <b>errno</b>,
and 0 on success.</p>
<p>The fetch function sets the <b>dptr</b> field of the returned
<b>datum</b> to NULL on failure, setting <b>errno</b>,
and returns a non-NULL <b>dptr</b> on success.</p>
<p>The store function returns -1 on failure, setting <b>errno</b>,
and 0 on success.</p>
<p>The delete function returns -1 on failure, setting <b>errno</b>,
and 0 on success.</p>
<p>The firstkey function sets the <b>dptr</b> field of the returned
<b>datum</b> to NULL on failure, setting <b>errno</b>,
and returns a non-NULL <b>dptr</b> on success.</p>
<p>The nextkey function sets the <b>dptr</b> field of the returned
<b>datum</b> to NULL on failure, setting <b>errno</b>,
and returns a non-NULL <b>dptr</b> on success.</p>
<br><b>Errors</b>
<p>The dbminit, fetch, store, delete, firstkey, and nextkey functions may
fail and return an error for errors specified for other Berkeley DB and C
library or system functions.</p>
<b>Ndbm Diagnostics</b>
<p>The dbm_close method returns non-zero when an error has occurred reading or
writing the database.</p>
<p>The dbm_close method resets the error condition on the named database.</p>
<p>The dbm_open function returns NULL on failure, setting <b>errno</b>,
and a DBM reference on success.</p>
<p>The dbm_fetch function sets the <b>dptr</b> field of the returned
<b>datum</b> to NULL on failure, setting <b>errno</b>,
and returns a non-NULL <b>dptr</b> on success.</p>
<p>The dbm_store function returns -1 on failure, setting <b>errno</b>,
0 on success, and 1 if DBM_INSERT was set and the specified key already
existed in the database.</p>
<p>The dbm_delete function returns -1 on failure, setting <b>errno</b>,
and 0 on success.</p>
<p>The dbm_firstkey function sets the <b>dptr</b> field of the returned
<b>datum</b> to NULL on failure, setting <b>errno</b>,
and returns a non-NULL <b>dptr</b> on success.</p>
<p>The dbm_nextkey function sets the <b>dptr</b> field of the returned
<b>datum</b> to NULL on failure, setting <b>errno</b>,
and returns a non-NULL <b>dptr</b> on success.</p>
<p>The dbm_close function returns -1 on failure, setting <b>errno</b>,
and 0 on success.</p>
<p>The dbm_close function returns -1 on failure, setting <b>errno</b>,
and 0 on success.</p>
<br><b>Errors</b>
<p>The dbm_open, dbm_close, dbm_fetch, dbm_store, dbm_delete, dbm_firstkey,
and dbm_nextkey functions may fail and return an error for errors
specified for other Berkeley DB and C library or system functions.</p>
</tt>
<table width="100%"><tr><td><br></td><td align=right>
<a href="../api_c/api_core.html"><img src="../images/api.gif" alt="API"></a><a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a>
</td></tr></table>
<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
</body>
</html>
